//
//  SubjectViewController.m
//  MyLittleBrother
//
//  Created by speeda on 17/7/30.
//  Copyright © 2017年 speeda. All rights reserved.
//

#import "SubjectViewController.h"
#import "HomeCell.h"
#import "StrangeThingViewController.h"
#import "ArticleModel.h"
#import "UIView+Extend.h"

@interface SubjectViewController () <UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UIButton *titleButton;
@property (weak, nonatomic) IBOutlet UITableView *subjectTableView;
@property (nonatomic, strong) NSMutableArray *subjectArray;

@property (weak, nonatomic) IBOutlet UIButton *hotButton;
@property (weak, nonatomic) IBOutlet UIButton *allButton;
@property (weak, nonatomic) IBOutlet UIView *lineView;

@property (nonatomic, assign) NSInteger page;

@end

@implementation SubjectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNavigationItem];
    
    [self initTableView];
    
    [self hotTopicBtnAction:self.hotButton];
    
}

- (void)initTableView {

    [self.titleButton setTitle:[NSString stringWithFormat:@"#%@#",self.titleStr] forState:UIControlStateNormal];
    
    self.subjectTableView.dataSource = self;
    self.subjectTableView.delegate = self;
    self.subjectTableView.tableFooterView = [[UIView alloc] init];
    self.subjectTableView.estimatedRowHeight = 100;
    self.subjectTableView.rowHeight = UITableViewAutomaticDimension;
    
    __weak typeof(self) weakSelf = self;
    //默认block方法：设置下拉刷新
    self.subjectTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf AFNNewData];
    }];
    [self.subjectTableView.mj_header beginRefreshing];
    
    //默认block方法：设置上拉加载更多
    self.subjectTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf AFNMoreData];
    }];
    
}


- (void)setNavigationItem {

    UIButton *rightBtton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtton addTarget:self action:@selector(rightItemAction) forControlEvents:UIControlEventTouchUpInside];
    [rightBtton setImage:[UIImage imageNamed:@"btn_write-1"] forState:UIControlStateNormal];
    [rightBtton sizeToFit];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtton];
    self.navigationItem.rightBarButtonItem = rightItem;
}

- (void)rightItemAction {

    StrangeThingViewController *strangeThingVc = [[StrangeThingViewController alloc] init];
    strangeThingVc.topicid = self.topicid;
    strangeThingVc.topicString = self.titleStr;
    [self.navigationController pushViewController:strangeThingVc animated:YES];
}



#pragma mark -- 获取贴子
- (void)AFNNewData {
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"command"] = @"getarticle";
    dict[@"topicid"] = self.topicid;
    dict[@"circleid"] = @"";
    dict[@"page"] = @"1";
    
    [AFHTTPSTool POSTRequestRequestWebsite:Url AndDictionary:dict success:^(NSURLSessionDataTask *dataTask, id responseObject) {
        
        [self.subjectTableView.mj_header endRefreshing];
        
        NSDictionary *dic = responseObject;
        NSString *state = [NSString stringWithFormat:@"%@",dic[@"code"]];
        if ([state isEqualToString:@"000000"]) {
            
            NSLog(@"获取贴子 = %@",responseObject);
            [self.subjectArray removeAllObjects];
            
            NSArray *array = [ArticleModel mj_objectArrayWithKeyValuesArray:[dic[@"data"] objectForKey:@"rows"]];
            
            NSString *pageString = [dic[@"data"] objectForKey:@"page"];
            _page = pageString.integerValue;
            _page++;
            
            [self.subjectArray addObjectsFromArray:array];
            
            [self.subjectTableView reloadData];
            
        } else {
            
            NSString *errorText = [NSString stringWithFormat:@"%@",dic[@"msg"]];
            [SVProgressHUD showInfoWithStatus:errorText];
        }
        
        
    } failure:^(NSURLSessionDataTask *dataTask, NSError *error) {
        
        [self.subjectTableView.mj_header endRefreshing];
        [SVProgressHUD showErrorWithStatus:error.localizedDescription];
    }];
}

- (void)AFNMoreData {
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"command"] = @"getarticle";
    dict[@"topicid"] = self.topicid;
    dict[@"circleid"] = @"";
    dict[@"page"] = @(_page);
    
    [AFHTTPSTool POSTRequestRequestWebsite:Url AndDictionary:dict success:^(NSURLSessionDataTask *dataTask, id responseObject) {
        
        [self.subjectTableView.mj_footer endRefreshing];
        
        NSDictionary *dic = responseObject;
        NSString *state = [NSString stringWithFormat:@"%@",dic[@"code"]];
        if ([state isEqualToString:@"000000"]) {
            
            NSLog(@"获取贴子 = %@",responseObject);
            NSArray *array = [ArticleModel mj_objectArrayWithKeyValuesArray:[dic[@"data"] objectForKey:@"rows"]];
            NSString *pageString = [dic[@"data"] objectForKey:@"page"];
            _page = pageString.integerValue;
            _page++;
            
            [self.subjectArray addObjectsFromArray:array];
            
            [self.subjectTableView reloadData];
            
        } else {
            
            NSString *errorText = [NSString stringWithFormat:@"%@",dic[@"msg"]];
            [SVProgressHUD showInfoWithStatus:errorText];
        }
        
        
    } failure:^(NSURLSessionDataTask *dataTask, NSError *error) {
        
        [self.subjectTableView.mj_footer endRefreshing];
        [SVProgressHUD showErrorWithStatus:error.localizedDescription];
    }];
}



#pragma mark -- UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.subjectArray.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellID = @"cellID";
    HomeCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"HomeCell" owner:nil options:nil];
        cell = [nibs firstObject];
    }
    ArticleModel *articleItem = self.subjectArray[indexPath.row];
    cell.articleItem = articleItem;
    
    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    
}


//热门
- (IBAction)hotTopicBtnAction:(UIButton *)sender {
    
    sender.selected = YES;
    self.allButton.selected = NO;
    [UIView animateWithDuration:0.2 animations:^{
        self.lineView.centerX = sender.centerX;
    }];
    
    [self.subjectArray removeAllObjects];
    [self.subjectTableView reloadData];
    [self.subjectTableView.mj_header beginRefreshing];
}

//全部
- (IBAction)allTopicBtnAction:(UIButton *)sender {
    
    sender.selected = YES;
    self.hotButton.selected = NO;
    [UIView animateWithDuration:0.2 animations:^{
        self.lineView.centerX = sender.centerX;
    }];
    
    [self.subjectArray removeAllObjects];
    [self.subjectTableView reloadData];
    [self.subjectTableView.mj_header beginRefreshing];
}


- (NSMutableArray *)subjectArray {
    
    if (!_subjectArray) {
        _subjectArray = [NSMutableArray array];
    }
    
    return _subjectArray;
}

@end
